import {WL} from "./weilapb";
import { WLBuildMsgRet } from "./weilapb_wrapper_data"
import { WL_LocationControl, WL_LocationInfo, WL_SessionInfo } from "./weilapb_data_define";
import { BuildWeilaMsg } from "./weilapb_wrapper";

class WeilaPbLocationWrapper {
    public static buildReportLocationReq(locationInfo: WL_LocationInfo,
                                         sessionInfoList: WL_SessionInfo[],
                                         appId: string,
                                         extensionType: number,
                                         extensionBody?: string): WLBuildMsgRet {
        const locationMessage = new WL.Location.LocationMessage();
        locationMessage.reqReportLocation = new WL.Location.ReqReportLocation();
        locationMessage.reqReportLocation.appid = appId;
        locationMessage.reqReportLocation.extensionType = extensionType;
        locationMessage.reqReportLocation.extensionBody = extensionBody;
        locationMessage.reqReportLocation.locationInfo = locationInfo;
        
        locationMessage.reqReportLocation.sessionInfos = [];
        sessionInfoList.forEach(value => {
            locationMessage.reqReportLocation!.sessionInfos!.push({sessionId: value.sessionId, sessionType: value.sessionType});
        });

        return BuildWeilaMsg.buildWeilaMsgReq(WL.Service.ServiceID.SERVICE_LOCATION, WL.Location.LocationCommandId.LOCATION_COMMAND_REPORT_LOCATION,
            WL.Service.CommandType.COMMAND_REQUEST, locationMessage);
    }

    public static buildGetLocationReq(sessionInfo: WL_SessionInfo, userIdList: number[]): WLBuildMsgRet {
        const locationMessage = new WL.Location.LocationMessage();
        locationMessage.reqGetLocation = new WL.Location.ReqGetLocation();
        locationMessage.reqGetLocation.sessionInfo = sessionInfo;
        if (userIdList.length) {
            locationMessage.reqGetLocation.userIds = Array.from(userIdList);
        }

        return BuildWeilaMsg.buildWeilaMsgReq(WL.Service.ServiceID.SERVICE_LOCATION, WL.Location.LocationCommandId.LOCATION_COMMAND_GET_LOCATION,
            WL.Service.CommandType.COMMAND_REQUEST, locationMessage);
    }

    public static buildLocationControlReq(sessionInfo: WL_SessionInfo, locationControl: WL_LocationControl, userIdList: number[]): WLBuildMsgRet {
        const locationMessage = new WL.Location.LocationMessage();
        locationMessage.reqLocationControl = new WL.Location.ReqLocationControl();
        locationMessage.reqLocationControl.locationControl = locationControl;
        locationMessage.reqLocationControl.sessionInfo = sessionInfo;
        locationMessage.reqLocationControl.userIds = Array.from(userIdList);

        return BuildWeilaMsg.buildWeilaMsgReq(WL.Service.ServiceID.SERVICE_LOCATION, WL.Location.LocationCommandId.LOCATION_COMMAND_LOCATION_CONTROL,
            WL.Service.CommandType.COMMAND_REQUEST, locationMessage);
    }
}

export {WeilaPbLocationWrapper}
